package com.websocketOfNotice;


import com.domain.Issue;
import com.domain.IssueComment;
import com.domain.IssueCommentResponse;
import com.domain.Notice;
import com.mapper.helping.IssueCommentMapper;
import com.mapper.helping.IssueCommentResponseMapper;
import com.mapper.helping.IssueMapper;
import com.mapper.helping.NoticeMapper;
import com.websocketOfNotice.msgDomain.InteractiveMessage;
import com.websocketOfNotice.msgDomain.NoticeType;
import com.websocketOfNotice.msgDomain.SendMessage;
import living.com.domain.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步发送互动消息
 * @author zdh
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class InteractiveMessageService {
  private final NoticeMapper noticeMapper;
  private final IssueMapper issueMapper;
  private final IssueCommentMapper issueCommentMapper;
  private final IssueCommentResponseMapper issueCommentResponseMapper;

  /**
   *  my触发事件的用户--一定是外边传过来,UserUtil存在线程隔离机制,拿到的结果为Null
   *  当前用户,事件id,事件类型
   */
  @Async("sendMessage")
  public void notice(User my, int id, String type,int special_u_id,int special_issue_id) {
    NoticeType res = null;
    //根据事件类型和事件id查询出事件关联的用户id
    int u_id=-1;
    //问题id
    int issue_id=-1;
    switch (type) {
      case "COMMENT_ON_ISSUE": {
        res = NoticeType.COMMENT_ON_ISSUE;
        Issue issue = issueMapper.selectById(id);
        u_id=issue.getUserId();
        issue_id=issue.getId();
        break;
      }
      case "COMMENT_ON_COMMENT": {
        res = NoticeType.COMMENT_ON_COMMENT;
        IssueComment issueComment = issueCommentMapper.selectById(id);
        u_id=issueComment.getUserId();
        issue_id=issueComment.getIssueId();
        break;
      }
      case "LIKE_ON_SON_COMMENT":
      {
        res = NoticeType.LIKE_ON_SON_COMMENT;
        IssueCommentResponse issueCommentResponse = issueCommentResponseMapper.selectById(id);
        Integer fromUserId = issueCommentResponse.getFromUserId();
        u_id=fromUserId;
        //匿名版主发布的评论
        if(u_id==0)
        {
          u_id=issueCommentMapper.selectById(issueCommentResponse.getFatherCommentId()).getUserId();
        }
        issue_id=issueCommentMapper.selectById(issueCommentResponse.getFatherCommentId()).getIssueId();
        break;
      }
      case "COMMENT_ON_SON_OR_GRANTSON_COMMENT": {
        res = NoticeType.COMMENT_ON_SON_OR_GRANTSON_COMMENT;
        u_id=special_u_id;
        //问题id赋值--特殊处理
        issue_id=special_issue_id;
        break;
      }
      case "LIKE_ON_ISSUE": {
        res = NoticeType.LIKE_ON_ISSUE;
        Issue issue = issueMapper.selectById(id);
        u_id=issue.getUserId();
        issue_id=issue.getId();
        break;
      }
      case "LIKE_ON_COMMENT": {
        res = NoticeType.LIKE_ON_COMMENT;
        IssueComment issueComment = issueCommentMapper.selectById(id);
        u_id=issueComment.getUserId();
        issue_id=issueComment.getIssueId();
        break;
      }
      default:
        throw new IllegalArgumentException("通知类型错误");
    }
    noticeOperation(my, issue_id,id, u_id,res);
  }


  /**
   * <p>
   *     通知通用处理方法
   * </p>
   * @param my 触发事件的用户
   * @param id 事件id和事件关联用户u_id
   */
  private void noticeOperation(User my,int issue_id, int id,int u_id,NoticeType noticeType) {
    //自己触发事件,就不需要通知自己了
    if (my.getId() != u_id) {
      boolean isSuccess = WebSocket.sendMessage(u_id,
          new SendMessage<>(
                  //通知类型
                  noticeType.getType(),
              //携带的数据--互动消息
              new InteractiveMessage(issue_id,
                      //事件id
                      id,
                  my.getId(), my.getUsername(), my.getAvatar(), noticeType.getType())));
      //发送失败--保存到数据库,等到用户登录的时候，取出来告诉他
      if (!isSuccess) {
        noticeMapper.insert(Notice.builder().type(noticeType.getType())
                .fromUserId(my.getId()).toUserId(u_id).issueId(issue_id)
                .eventId(id).build());
      }
    }
  }


}
